<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//用户组角色管理表
class Bbs_user_group_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->table = 'bbs_user_group';
        $this->bbs_user_permission = "bbs_user_permission";
    }
    
    function get($where){
        $this->db->select($this->table.'.*');
        $this->db->from($this->table);
        $this->db->where($where);
        $this->db->order_by('id desc');
        $query = $this->db->get();
        return $query->row_array(); 
    }
    
    function get_list($where,$page,$limit){
        $this->db->select($this->table.'.*');
        $this->db->from($this->table);
        $this->db->where($where);
        $this->db->limit($limit, $page);
        $this->db->order_by('id asc');
        $query = $this->db->get();
        return $query->result_array(); 
    }
    
    //插入角色组类型
    function insert($data)
    {
        $this->db->insert($this->table, $data);
        if($this->db->affected_rows()){
           return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    //更新
    function update($data,$id)
    {
            return $this->db->where('id',$id)->update($this->table, $data);
    }
    
    public function getpermissionlist($pid)
    {
        $this->db->select('*');
        $this->db->from($this->bbs_user_permission);
        $this->db->where('pid',$pid);
        $query = $this->db->get();
        return $query->result_array();
    }
    
    public function GetPermissionAll()
    {
        $this->db->select('*');
        $this->db->from($this->bbs_user_permission);
        $this->db->order_by('id asc');
        $query = $this->db->get();
        return $query->result_array();
    }
    
    //递归生成树形数组
    public function tree($table,$pid='0') {
            $tree = array();
            foreach($table as $row){
                    if($row['pid']==$pid){
                            $tmp = $this->tree($table,$row['id']);
                            if($tmp){
                                    $row['children']=$tmp;
                            }else{
                                    $row['leaf'] = true;
                            }
                            $tree[]=$row;                
                    }
            }
            return $tree;        
    }
    
    function get_permission($where){
        $this->db->select($this->bbs_user_permission.'.*');
        $this->db->from($this->bbs_user_permission);
        $this->db->where($where);
        $this->db->order_by('id desc');
        $query = $this->db->get();
        return $query->row_array(); 
    }
    
    function permission_insert($data)
    {
        $this->db->insert($this->bbs_user_permission, $data);
        if($this->db->affected_rows()){
           return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    function permission_update($data,$id)
    {
            return $this->db->where('id',$id)->update($this->bbs_user_permission, $data);
    }
}
